package com.lwl.controller;


import com.github.pagehelper.PageInfo;
import com.lwl.entity.MyPage;
import com.lwl.entity.Tuser;
import com.lwl.entity.LayData;
import com.lwl.service.ITuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Lei
 * @create 2022-10-19 10:08
 */

@Controller
public class TuserController {

    @Autowired
    private ITuserService tuserService;

    /**
     * 通过表单 添加/修改 用户信息
     * @param tuser
     * @return
     */
    @PostMapping("registerTest")
    @ResponseBody
    public LayData registerTest(Tuser tuser){
        String imgpath = tuser.getImgpath();
        System.out.println("图片上传的地址是imgpath = " + imgpath);
        Boolean aBoolean = tuserService.registerUser(tuser);
        LayData layData = new LayData(aBoolean);
        return layData;
    }

    /**
     * 查询所有的用户信息并进行分页展示
     * @param myPage
     * @param tuser
     * @return
     */
    @GetMapping("query")
    @ResponseBody
    public LayData queryUser(MyPage myPage,Tuser tuser){

        PageInfo pageInfo = tuserService.queryAll(myPage,tuser);

        return new LayData(pageInfo);
    }

    /**
     * 用户状态栏中根据用户的状态按钮进行修改用户的状态
     * @param tuser
     * @return
     */
    @PostMapping("updUser")
    @ResponseBody
    public LayData updUser(Tuser tuser){
        boolean status = tuserService.updUser(tuser);
        return new LayData(status);
    }

    /**
     * 添加用户时，查询数据库是否有相同的用户信息，如果有同名用户，则不能重复添加
     * @param username
     * @return
     */
    @PostMapping("queryByName")
    @ResponseBody
    public LayData queryByName(String username){
        return new LayData(tuserService.queryByName(username));
    }

    /**
     * 主菜单的用户基本资料功能
     * @param username
     * @return
     */
    @PostMapping("queryByUserName")
    @ResponseBody
    public LayData queryByUserName(String username){
        System.out.println("开始查询用户基本信息");
        System.out.println("username = " + username);
        Tuser tuser = tuserService.queryByUserName(username);
        return new LayData(tuser);
    }

    /**
     * 主菜单的修改用户密码功能
     * @return
     */
    @PostMapping("updatePass")
    @ResponseBody
    public LayData updatePass(Tuser tuser,String newpassword){
        boolean status = tuserService.updatePass(tuser,newpassword);
        return new LayData(status);
    }
}
